package com.baosight.sczx.zm.cn.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCP;
import com.baosight.sczx.zm.cn.domain.ZMCNMccy;
import com.baosight.sczx.zm.cn.domain.ZMCNYdzhcyMain;
/**
 * 煤层煤样结果管理
 */
public class ServiceZMCN21 extends ServiceEPBase {
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p> 
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		
		
		return inInfo;
	}
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p> 
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdYear=inInfo.getString("fdYear");
//		String fdReportDepartmentId=inInfo.getString("fdReportDepartmentId");
		if(fdYear==null||fdYear.trim().equals("")){
			fdYear=inInfo.getCellStr("inqu_status", 0, "Year");
		}
		if(fdYear!=null && !fdYear.trim().equals("")){
			inInfo.setCell("inqu_status", 0, "fdYear", fdYear);
		}
//		inInfo.setCell("inqu_status", 0, "fdReportDepartmentId", fdReportDepartmentId);
		EiInfo outInfo = super.query(inInfo, "ZMCNMccy.query", new ZMCNMccy());
		
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.coalKind", true));// 煤种
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.ch.projectId", true));// 工作面
		outInfo.addBlock(getMines()); 
		outInfo.addBlock(StaticBlocks.getStauts4());
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		return outInfo;
	}	
	
	public EiBlock getMines() {
		EiBlock block = new EiBlock("cmes.ch.mine");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);

		List<Map> _listData = new ArrayList<Map>();
		try {
			_listData = dao.query("ZMCNMccyMain.queryTreeChild", null);
		} catch (Exception e) {}
		
		for(int i = 0; i < _listData.size(); i++) {
			_list.add(_listData.get(i)); 
		} 
		block.setRows(_list);
		return block;
	}
	
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p> 
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		EiInfo outinfo = new EiInfo();
		ZMCNMccy _dom = new ZMCNMccy();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();

		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CN_MCCY"));
			String fdYear = _dom.getFdYear();
			if(fdYear==null || fdYear.trim().equals("")){
				fdYear=inInfo.getCellStr("inqu_status", 0, "Year");
			}
			_dom.setFdYear(fdYear);
			this.dao.insert("ZMCNMccy.insert", _dom);
		}
		outinfo = this.query(inInfo);
		outinfo.setStatus(1);
		outinfo.setMsg("操作" + rowcount + "记录成功");
		return outinfo;
	}
	 
	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p> 
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo outInfo = new EiInfo();
		ZMCNMccy _dom = new ZMCNMccy();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
//			boolean validate = this.validate1(inInfo, _dom, params);
//			if(validate){
//				return inInfo;
//			}
//			String fdYear = _dom.getFdYear();
//			if(fdYear==null || fdYear.trim().equals("")){
//				fdYear=inInfo.getCellStr("inqu_status", 0, "Year");
//			}
//            params.put("fdYear", fdYear);
//			validate = this.validate(inInfo, _dom, params);
//			if(validate){
//				return inInfo;
//			}
//			_dom.setFdYear(fdYear);
			this.dao.update("ZMCNMccy.update", _dom);
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p> 
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo outInfo = new EiInfo();
		ZMCNMccy _dom = new ZMCNMccy();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		Map<String, String> params = new HashMap<String, String>();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			params.put("fdKeyId", _dom.getFdKeyId());
			boolean validate = this.validate1(inInfo, _dom, params);
			if(validate){
				outInfo =this.query(inInfo);
				return outInfo;
			}else{
				this.dao.delete("ZMCNMccy.delete", _dom);
			}
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}
	
	/**
	 * <p>Function: report</p>
	 * <p>Description: 上报</p> 
	 */
	public EiInfo report(EiInfo inInfo) {
		super.update(inInfo, "ZMCNMccy.report");
		EiBlock eiBlock = inInfo.getBlock("result");
		ZMCNMccy _dom = new ZMCNMccy();
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		EiInfo outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}
	
	/**
	 * 校验是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-06-28 14:12:464 李红星
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo validate(EiInfo inInfo) {
		ZMCNMccy _dom = new ZMCNMccy();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		String oper=(String)inInfo.get("efCurButtonEname");
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String> params = new HashMap<String, String>();
			String fdWorkfaceName = _dom.getFdWorkfaceName();
			String fdMyName=_dom.getFdMyName();
			String fdYear = _dom.getFdYear();
			String fdMyDate=_dom.getFdMyDate();
			if(fdYear==null || fdYear.trim().equals("")){
				fdYear=inInfo.getCellStr("inqu_status", 0, "Year");
			}
			params.put("fdYear", fdYear);
			params.put("fdWorkfaceName", fdWorkfaceName);
			params.put("fdMyName", fdMyName);
			params.put("fdMyDate", fdMyDate);
	//		String fdYear=params.get("fdYear");
			if(oper.equalsIgnoreCase("update1")||oper.equalsIgnoreCase("update"))
			{
				params.put("fdKeyId", _dom.getFdKeyId());
			}
			List result = dao.query("ZMCNMccy.validate", params);
			if(result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				ZMCNMccy _temp = (ZMCNMccy)result.get(0);
			
				StringBuffer msg = new StringBuffer("提交失败，");
	/*			if(_temp.getFdMiningName() != null && _temp.getFdMiningName().equals(fdMiningName)){
					msg.append(" 矿名");
				}*/
	/*			if(_temp.getFdYear() != null && _temp.getFdYear().equals(fdYear)) {
					msg.append(" 年份 ");
				}*/
				if(_temp.getFdMyDate() != null && _temp.getFdMyDate().equals(fdMyDate)) {
					msg.append(" 检测日期 ");
				}
				if(_temp.getFdMyName() != null && _temp.getFdMyName().equals(fdMyName)) {
					msg.append(" 煤样名称 ");
				}
				if(_temp.getFdWorkfaceName() != null && _temp.getFdWorkfaceName().equals(fdWorkfaceName)) {
					msg.append(" 工作面名称 ");
				}
				msg.append("重复！");
			
				inInfo.setMsg(msg.toString());
				return inInfo;
			}
		}
		inInfo.setStatus(1);
		return inInfo;
	}
	
	/**
	 * 校验是否可以操作
	 * <p>Function: validate</p>
	 * <p>Description: 明细已经生效时，不允许修改或者删除</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-6-30 09:45:41 李红星
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate1(EiInfo inInfo, ZMCNMccy _dom, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		List result = dao.query("ZMCNMccy.validate2", params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCNMccy _temp = (ZMCNMccy)result.get(0);
		
			StringBuffer msg = new StringBuffer("提交失败，");
			msg.append("数据已经上报，不能修改或删除！");
			
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}
} 